package com.qf.controller;

import com.qf.Resp.BaseResp;
import com.qf.loanapplication.Loanapplication;
import com.qf.service.LoanapplicationService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
public class LoanapplicationController {
    @Autowired
    LoanapplicationService loanapplicationService;
    /////////////////后台
    //查所有
    @RequestMapping("/selectByOperatorIdAndStatus")
    public List<Loanapplication> selectByOperatorIdAndStatus(@RequestBody Map map){
        Integer operatorId = (Integer) map.get("operatorId");
        String status = (String) map.get("status");
        Loanapplication loanapplication = new Loanapplication();
        loanapplication.setOperatorId(operatorId);
        loanapplication.setStatus(status);
        List<Loanapplication> select = loanapplicationService.select(loanapplication);
        return select;
    }
    //审核借款
    @RequestMapping("/updateOne")
    public BaseResp updateOne(@RequestBody Loanapplication loanapplication){

        Date startTime = loanapplication.getStartTime();
        String status = loanapplication.getStatus();
        Integer id = loanapplication.getId();
        BaseResp baseResp = new BaseResp();
        Integer operatorId=2;
        if(id==null||startTime==null||operatorId==null){
            baseResp.setCode(400l);
            baseResp.setMessage("修改失败");
        }else {
            loanapplication.setOperatorId(operatorId);
            int update = loanapplicationService.update(loanapplication);
            if(update==0) {
                baseResp.setCode(400l);
                baseResp.setMessage("修改失败");
            }else{
                baseResp.setCode(200l);
                baseResp.setMessage("审核成功");
            }
        }
        return baseResp;
    }
    //根据id删除
    @RequestMapping("/deleteByLooanapplicationId")
    public BaseResp deleteById(@RequestBody Map map){
        Integer id = (Integer) map.get("id");
        System.out.println(id);
        List<Integer> list=new ArrayList<>();
        list.add(id);
        int delete = loanapplicationService.delete(list);
        BaseResp baseResp=new BaseResp();
        if(delete==0){
            baseResp.setCode(400l);
            baseResp.setMessage("删除失败");
        }else {
            baseResp.setCode(200l);
            baseResp.setMessage("删除成功");
        }
        return baseResp;
    }



    //前台业务
    //申请借款
    @RequestMapping("/insertOne")
    public BaseResp insertOne(@RequestBody Loanapplication loanapplication,HttpServletRequest request){
        Integer borrowerId=2;
        //用户id
        loanapplication.setBorrowerId(borrowerId);
        //申请时间
        long l = System.currentTimeMillis();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date(l);
        loanapplication.setCreateTime(date);
        //默认状态
        loanapplication.setStatus("审核中");
        int insert = loanapplicationService.insert(loanapplication);
        BaseResp baseResp = new BaseResp();
        if(insert==1){
            baseResp.setCode(200l);
            baseResp.setMessage("申请成功，待审核");

            //减少可贷款的额度
        }else {
            baseResp.setCode(400l);
            baseResp.setMessage("申请失败，请重新尝试");
        }
        return baseResp;
    }
    @RequestMapping("/selectAll")
    public BaseResp selectAll(@RequestBody Map map){
        Integer id = (Integer) map.get("id");
        Integer borrowerId = (Integer) map.get("borrowerId");
        Integer operatorId = (Integer) map.get("operatorId");
        String loanPurpose = (String) map.get("loanPurpose");
        Double loanAmount = (Double) map.get("loanAmount");
        Integer loanDuration = (Integer) map.get("loanDuration");
        String status = (String) map.get("status");

        Loanapplication loanapplication = new Loanapplication();
        loanapplication.setId(id);
        loanapplication.setBorrowerId(borrowerId);
        loanapplication.setOperatorId(operatorId);
        loanapplication.setLoanPurpose(loanPurpose);
        loanapplication.setLoanAmount(loanAmount);
        loanapplication.setLoanDuration(loanDuration);
        loanapplication.setStatus(status);

        List<Loanapplication> select = loanapplicationService.select(loanapplication);
        BaseResp baseResp = new BaseResp();
        if(select==null){
            baseResp.setCode(400l);
            baseResp.setMessage("失败");
        }else{
            baseResp.setCode(200l);
            baseResp.setMessage(select);
            baseResp.setTotal((long)select.size());
        }
        return baseResp;
    }

    //根据用户id查借款账单
    @RequestMapping("/selectByUid")
    public BaseResp selectByUid(@RequestBody Map map){
        Integer id=(Integer)map.get("borrowerId");

        Loanapplication loanapplication = new Loanapplication();
        loanapplication.setBorrowerId(id);
        List<Loanapplication> select = loanapplicationService.select(loanapplication);

        BaseResp baseResp = new BaseResp();
        if(select==null){
            baseResp.setCode(400l);
            baseResp.setMessage("无需还款");
            return  baseResp;
        }else{
            baseResp.setCode(200l);
            baseResp.setMessage(select);
            baseResp.setTotal((long)select.size());
        }
        return null;
    }



    public Integer getBorrowerId(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        Integer borrowerId=null;
        if(cookies!=null){
            for(Cookie cookie:cookies){
                if (cookie.getName().equals("borrowerId")) {
                    borrowerId = Integer.valueOf(cookie.getValue()) ;
                }
            }
        }
        return borrowerId;
    }


    /*李卓池贷款账单*/
    @RequestMapping("/reOrder")
    public List<Loanapplication> reOrder(@RequestBody Map map){
        //BaseResp baseResp=new BaseResp();
        Loanapplication loanapplication=new Loanapplication();
        Integer id = (Integer) map.get("borrowerId");
        String status = (String) map.get("status");
        loanapplication.setBorrowerId(id);
        loanapplication.setStatus(status);
        List<Loanapplication> select = loanapplicationService.select(loanapplication);
        System.out.println(select.toString());
        System.out.println(select.size());
        //long size = (long)select.size();
        //baseResp.setCode(200l);
        //baseResp.setMessage(select);
        //baseResp.setTotal(size);
        return select;

    }

    /*根据订单号查询出某条具体的订单时间*/
    @RequestMapping("/findTime")
    public Date findTime(@RequestBody Map map){
        Integer id = (Integer) map.get("id");
        Loanapplication loanapplication=new Loanapplication();
        loanapplication.setId(id);
        List<Loanapplication> select = loanapplicationService.select(loanapplication);
        Date startTime=null;
        for(Loanapplication lo:select){
             startTime = lo.getStartTime();
        }
    return startTime;
    }


}
